package com.qpf.mycms.manager.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qpf.mycms.api.pojo.PageResult;
import com.qpf.mycms.api.pojo.ResponseResult;
import com.qpf.mycms.api.pojo.UserRequest;
import com.qpf.mycms.api.pojo.dto.UserDto;
import com.qpf.mycms.manager.sys.domain.SysUser;
import com.qpf.mycms.manager.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/sys/user")
@Tag(name = "用户控制器", description = "用户控制器描述")
public class SysUserController {
    @Resource
    private SysUserService service;

    @Operation(
            summary = "根据id查询用户",
            parameters = {
                    @Parameter(name = "id", description = "用户标识号", required = true, example = "1")
            },
            responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(

                    mediaType = "application/json",
                    schema = @Schema(
                            title = "ResponseResult和UserDto组合模型",
                            description = "返回实体，ResponseResult内data为UserDto模型",
                            anyOf = {ResponseResult.class, UserDto.class})
            )),
            @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    @GetMapping("/{id}")
    public ResponseResult<UserDto> detail(@PathVariable Long id) {
        UserRequest request = new UserRequest();
        request.setId(id);
        SysUser user = service.detail(request);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);
        return ResponseResult.success(userDto);
    }
    @Operation(
            summary = "分页查询用户",
            parameters = {
                    @Parameter(name = "request", description = "用户实体", required = true, example = "{\n" +
                            "   \"pageNo\": 1,\n" +
                            "   \"pageSize\": 10\n" +
                            "}")
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(
                                    title = "ResponseResult和Page组合模型",
                                    description = "返回实体，ResponseResult内data为Page模型",
                                    anyOf = {ResponseResult.class, Page.class})
                    )),
                    @ApiResponse(responseCode = "404", description = "用户不存在")
            })
    @GetMapping
    public ResponseResult<PageResult<UserDto>> findPage(@Validated UserRequest request) {
        PageResult<SysUser> page = service.findPage(request);
        PageResult<UserDto> userDtoPage = new PageResult<>();
        BeanUtils.copyProperties(page, userDtoPage);
        return ResponseResult.success(userDtoPage);
    }

    @PostMapping
    @Operation(description = "新增用户")
    public ResponseResult<?> add(@RequestBody @Validated({UserRequest.SysUserInsertInterface.class}) UserRequest request) {
        service.add(request);
        return ResponseResult.success();
    }
    @Operation(description = "修改用户")
    @PutMapping("/{id}")
    public ResponseResult<?> edit(@PathVariable Long id, @RequestBody UserRequest request) {
        request.setId(id);
        service.edit(request);
        return ResponseResult.success();
    }

    @Operation(description = "删除用户")
    @DeleteMapping("/{id}")
    public ResponseResult<?> delete(@PathVariable Long id) {
        UserRequest request = new UserRequest();
        request.setId(id);
        service.del(request);
        return ResponseResult.success();
    }
}
